import LoadManager from "../manager/LoadManager";

export default class HeroLogic {
    constructor()
    {

    }
    private static _instance:HeroLogic;
    
    public static get instance() : HeroLogic {
        if(!this._instance)
        {
            this._instance = new HeroLogic()
        }
        return this._instance;
    }
    private _heroList:Array<object> = JSON.parse(cc.sys.localStorage.getItem('heroDataList'))||[];
    
    public get heroList() : Array<object> {
        return this._heroList;
    }
    private date:Date = new Date()
    /**
     * 创建英雄
     */
    public createHeroObj(id:number,starLv:number,quality:number,) {
        let heroCfg:Array<object> = LoadManager.instance.getStaticConfigResByUrl("config/HeroConfig")
        let temp:object = heroCfg[id]
        if (!temp)
        {
            return
        }
        
        let heroObj:object = {}
        heroObj["h_id"] = "h"+this.date.getTime()
        heroObj["id"] = id
        heroObj["atk"] = temp["init_atk"]
        heroObj["def"] = temp["init_def"]
        heroObj["hp"] = temp["init_hp"]
        heroObj["mp"] = temp["init_mp"]
        heroObj["lv"] = 1
        heroObj["exp"] = 1
        heroObj["starLv"] = starLv
        heroObj["allot_point"] = 0
        heroObj["quality"] = quality
        return heroObj
    }
    /**
     * addHero
     */
    public addHero(heroObj:object) {
        this._heroList.push(heroObj)
        cc.sys.localStorage.setItem('heroDataList', JSON.stringify(this._heroList));
    }
    private initRandomHeroObj:object
    /**
     * 随机一个初始英雄
     */
    public onRandomeHero():object {
        let id:number = Math.floor(Math.random()*10)
        this.initRandomHeroObj = this.createHeroObj(id,1,1)
        return this.initRandomHeroObj
    }
    /**
     * 创建初始英雄
     */
    public createInitHero() {
        this.addHero(this.initRandomHeroObj)
    }
    /**
     * 升级英雄
     */
    public onLevelUpHero(h_id:number) {
        for (const key in this._heroList) {
            if (this._heroList.hasOwnProperty(key)) {
                const heroObj:object = this._heroList[key];
                if(heroObj["h_id"]==h_id)
                {
                    heroObj["lv"] = heroObj["lv"] + 1
                    return
                }
            }
        }
    }

}
